package springbook.user.dao;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

/**
 * Dao 객체를 생성해주는 DaoFactory 클래스
 * */

@Configuration
public class DaoFactory {
	
	/**
	 * userDao객체를 생성하는 메소드
	 * */	
	@Bean
	public UserDao userDao(){
		UserDao userDao = new UserDao();
		userDao.setDataSource(dataSource());
		return userDao;
	}
	
	/**
	 * accountDao객체를 생성하는 메소드
	 * */
	@Bean
	public AccountDao accountDao(){
		return  new AccountDao(connectionMaker());
	}
	
	/**
	 * messageDao객체를 생성하는 메소드
	 * */
	@Bean
	public MessageDao messageDao(){
		return  new MessageDao(connectionMaker());
	}
	
	/**
	 * D사의 DB connectionMaker
	 * */
	@Bean
	public ConnectionMaker connectionMaker(){
		return new DConnectionMaker();
	}
	
	/**
	 * D사의 connectionMaker를 이용하여 설정을 만들고 
	 * 이를 CountingConnectionMaker에서 사용하여
	 * 카운팅을 적용한다.
	 * */
	/*@Bean
	public CountingConnectionMaker connectionMaker(){
		return new CountingConnectionMaker(realConnectionMaker());
	}*/
	
	/**
	 * 개발용
	 * */
	/*
	@Bean
	public ConnectionMaker connectionMaker(){
		return new LocalDBConnectionMaker();
	}
	*/
	
	/**
	 * 운영용
	 * */
	/*
	@Bean
	public ConnectionMaker connectionMaker(){
		return new ProductionDBConnectionMaker();
	}
	*/
	
	/**
	 * dataSource로  DB연결 설정 method
	 * 
	 * */
	@Bean
	public DataSource dataSource(){
		SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
		dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
		dataSource.setUrl("jdbc:mysql://localhost/test");
		dataSource.setUsername("root");
		dataSource.setPassword("1234");
		
		return dataSource;
	}
}
